<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <div class="ai-icon"></div>
        </div>
        <h2>欢迎登录智周万物系统</h2>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item class="login-btn-group">
          <el-button type="primary" @click="handleLogin" class="login-btn">登录</el-button>
        </el-form-item>
        <div class="login-links">
          <router-link to="#" class="register-link">立即注册</router-link>
          <router-link to="#" class="forgot-link">忘记密码?</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  try {
    const response = await axios.post('/api/users/login', loginForm)
    if (response.data.token) {
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('username', response.data.username)
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    ElMessage.error(error.response?.data || '登录请求失败')
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #e0f7fa 0%, #b2ebf2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 420px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 15px;
  position: relative;
}

.ai-icon {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #4fc3f7 0%, #039be5 100%);
  border-radius: 50%;
  position: relative;
  overflow: hidden;
}

.ai-icon::after {
  content: 'AI';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  font-size: 32px;
  font-weight: bold;
}

.login-form {
  margin-top: 20px;
}

.code-btn {
  color: #039be5;
  border: 1px solid #039be5;
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  background: linear-gradient(135deg, #4fc3f7 0%, #039be5 100%);
  border: none;
}

.login-links {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.register-link, .forgot-link {
  color: #039be5;
  text-decoration: none;
  font-size: 14px;
}
</style>